package com.holly.common_view;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

/**
 * 标准的提示对话框
 *
 * @author Joey Huang（QQ:273179370）
 * @created 2016/9/21
 */
public class AlertDialog extends Dialog {
    private TextView mTvTitle;
    private TextView mTvMessage;
    private TextView mTvPositive;
    private TextView mTvNegative;
    private OnClickListener mNegativeListener;
    private OnClickListener mPositiveListener;

    public AlertDialog(Context context) {
        super(context, R.style.MAlertDialog);
        init();
    }

    private void init() {
        //点击屏幕外关闭
        setCanceledOnTouchOutside(true);
        Context themeContext = getContext();
        //设置为底部
        Window window = getWindow();
        window.setGravity(Gravity.CENTER);

        View view = LayoutInflater.from(themeContext).inflate(R.layout.dialog_alert_dialog, null);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        setContentView(view, layoutParams);
        initView(view);
    }

    private void initView(View view) {
        mTvTitle = (TextView) view.findViewById(R.id.tv_title_alert_dialog);
        mTvMessage = (TextView) view.findViewById(R.id.tv_message_alert_dialog);
        mTvPositive = (TextView) view.findViewById(R.id.tv_positive_alert_dialog);
        mTvNegative = (TextView) view.findViewById(R.id.tv_negative_alert_dialog);

        mTvPositive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (mPositiveListener != null) {
                    mPositiveListener.onClick(AlertDialog.this, 0);
                }
            }
        });
        mTvNegative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (mNegativeListener != null) {
                    mNegativeListener.onClick(AlertDialog.this, 0);
                }
            }
        });
    }

    public AlertDialog setNegativeButton(String text, final OnClickListener listener) {
        if (!isShowing()) {
            mTvNegative.setText(text);
        }
        mNegativeListener =listener;
        return this;
    }

    public AlertDialog setNegativeButton(String text) {
        if (!isShowing()) {
            mTvNegative.setText(text);
        }
        return this;
    }

    public AlertDialog setNegativeButton(final OnClickListener listener) {
        mNegativeListener =listener;
        return this;
    }

    public AlertDialog setPositiveButton(String text, final OnClickListener listener) {
        if (!isShowing()) {
            mTvPositive.setText(text);
        }
        mPositiveListener=listener;
        return this;
    }

    public AlertDialog setPositiveButton(final OnClickListener listener) {
        mPositiveListener=listener;
        return this;
    }

    public AlertDialog setTitle(String title) {
        if (!isShowing()) {
            mTvTitle.setText(title);
        }
        return this;
    }

    public AlertDialog setMessage(String message) {
        if (!isShowing()) {
            mTvMessage.setText(message);
        }
        return this;
    }
}
